<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Reports' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 搜索 -->
      <el-row :gutter="20">
        <el-col :span="10"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="info.query"
              class="input-with-select"
              clearable
              @clear="list"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="searchName"
              ></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="8"
          ><div class="grid-content bg-purple">
            <el-button type="primary" @click="append">添加按钮</el-button>
          </div></el-col
        >
      </el-row>

      <!-- 表格 -->

      <el-table
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px; margin-bottom: 20px"
      >
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="off(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="backfill(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              circle
              @click="allot(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 模态框 -->

      <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">添加</el-button>
        </span>
      </el-dialog>
      <!-- 修改模态框 -->
      <el-dialog title="修改用户" :visible.sync="dialogVisible1" width="50%">
        <el-form
          :model="ruleForm1"
          label-width="100px"
          class="demo-ruleForm"
          :rules="rules1"
          ref="ruleForm1"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm1.username" :disabled="true"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm1.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="ruleForm1.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>

          <el-button type="primary" @click="amend">修改</el-button>
        </span>
      </el-dialog>
      <!-- 分配角色的模态框 -->
      <el-dialog title="分配角色" :visible.sync="dialogVisible2" width="50%">
        <el-form :model="ruleForm2" label-width="80px">
          <el-form-item label="当前用户:">
            {{ ruleForm2.username }}
          </el-form-item>
          <el-form-item label="当前角色:">
            {{ ruleForm2.role_name }}
          </el-form-item>
          <el-form-item label="分配角色:">
            <el-select v-model="value" placeholder="请选择文章标签" filterable>
              <el-option
                v-for="item in castingList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="give">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //用户列表数据
      tableData: [],
      // 用户列表参数
      info: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 总条数
      total: 0,
      // 模态框标识
      dialogVisible: false,
      // 模态框标识
      dialogVisible1: false,
      // 模态框标识
      dialogVisible2: false,
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      ruleForm1: {},
      ruleForm2: {},
      // 分配角色关键字
      value: "",
      // 存放角色的列表数据
      castingList: [],
      // 正则校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3到 8个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确手机号码",
            trigger: ["blur", "change"],
          },
        ],
      },
      // 正则校验
      rules1: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确手机号码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },

  mounted() {
    this.list();
  },

  methods: {
    //渲染页面
    list() {
      this.$http.get("users", { params: this.info }).then((res) => {
        this.tableData = res.data.data.users;
        this.total = res.data.data.total;
    
      });
    },
    //修改状态
    off(row) {
      this.$http.put(`users/${row.id}/state/${row.mg_state}`).then((res) => {
        if (res.data.meta.status === 200) {
          this.$message.success(res.data.meta.msg);
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.info.pagesize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.info.pagenum = val;
      this.list();
    },
    // 输入搜索
    searchName() {
      this.info.pagenum = 1;
      this.list();
    },

    // 点击添加按钮显示模态框
    append() {
      this.dialogVisible = true;

      this.ruleForm = {};
    },
    // 添加
    add() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return this.$message("信息填入不正确");
        } else {
          this.$http.post("users", this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status === 201) {
              this.$message.success(res.data.meta.msg);
              this.list();
              this.dialogVisible = false;
            }
          });
        }
      });
    },
    // 点击编辑按钮回填信息
    backfill(row) {
      this.dialogVisible1 = true;
      this.ruleForm1 = row;
      console.log(this.ruleForm1);
    },
    amend() {
      this.$refs.ruleForm1.validate((valid) => {
        if (!valid) {
          return this.$message("信息填入不正确");
        } else {
          this.$http
            .put(`users/${this.ruleForm1.id}`, this.ruleForm1)
            .then((res) => {
              console.log(res);
              if (res.data.meta.status === 200) {
                this.$message.success(res.data.meta.msg);
                this.dialogVisible1 = false;
              }
            });
        }
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`users/${id}`).then((res) => {
            if (res.data.meta.status === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
             this.list()
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分配角色
    allot(row) {
      this.dialogVisible2 = true;
      this.ruleForm2 = {
        username: row.username,
        role_name: row.role_name,
        id: row.id,
      };
      this.$http.get("roles").then((res) => {
        this.castingList = res.data.data;
      });
    },
    // 点击分配角色
    give() {
      this.dialogVisible2 = false;
      this.$http
        .put(`users/${this.ruleForm2.id}/role`, { rid: this.value })
        .then((res) => {
          console.log(res);
          if (res.data.meta.status === 200) {
            this.$message.success(res.data.meta.msg);
            this.value = "";
          } else {
            this.$message.error(res.data.meta.msg);
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>